/* Copyright (c) 2024 Huawei Technologies Co., Ltd.
openFuyao is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN 'AS IS' BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */
import { ResponseCode } from '@/common/constants';
import { useLocation } from 'inula-router';
import { useEffect, useState } from 'openinula';
import { message } from 'antd';
import { ExportOutlined, RightOutlined, DownOutlined } from '@ant-design/icons';
import useLocalStorage from '@/hooks/useLocalStorage';
import '@/styles/rayLog.less';
import { getJobDetail } from '@/api/rayApi.js';
import { exportYamlOutPutLog } from '@/tools/utils';
export default function Log() {
  const [currentTheme, setCurrentTheme] = useLocalStorage('theme', 'light');
  const [messageApi, contextHolder] = message.useMessage();
  const location = useLocation();
  const jobName = location.state.name;
  const jobNameSpace = location.state.namespace;
  const [jobDetailLogData, setJobDetailLogData] = useState([]);
  const [openJobLog, setOpenJobLog] = useState();
  const [log, setLog] = useState();
  const getJobInfo = async () => {
    try {
      const res = await getJobDetail(jobNameSpace, jobName);
      if (res.data.code === ResponseCode.OK) {
        const arr = res.data.data.ray_head_logs.split('\n');
        setLog(res.data.data.ray_head_logs);
        arr.pop();
        setJobDetailLogData(arr);
      } else {
        let msg = '';
        msg = res.data.data ? `${res.data.msg}: ${res.data.data}` : res.data.msg;
        messageApi.error(`获取${jobName}详情失败,${msg}`);
      }
    } catch (error) {
      if (error) {
        let msg = '';
        msg = JSON.stringify(error.response.data.data) !== '{}' ? `${error.response.data.msg + ':' + error.response.data.data}` : error.response.data.msg;
        messageApi.error(`获取${jobName}详情失败,${msg}`);
      }
    }
  };
  const exportJobYaml = () => {
    exportYamlOutPutLog(`${jobName}`, log);
    messageApi.success('导出成功');
  };
  useEffect(() => {
    getJobInfo();
  }, []);
  useEffect(() => {
  }, [currentTheme]);
  return <div className="log">
    {contextHolder}
    <div className="log_top">
      <p className="log_top_title">日志信息</p>
      <p className="log_top_copy" onClick={exportJobYaml}><ExportOutlined style={{ color: '#3f66f5', marginRight: '4px' }} />导出</p>
    </div>
    <div className='log_contents'>
      <div className='log_content'>
        {(jobDetailLogData && jobDetailLogData.length > 0) ? jobDetailLogData.map((item, index) => (
          <div className='log_content_single'>
            <p className='log_content_single_line'></p>
            <RightOutlined className='log_content_single_arrow' style={{ display: openJobLog === (index + 1) ? 'none' : 'block' }} onClick={() => openJobLog ? setOpenJobLog() : setOpenJobLog(index + 1)} />
            <DownOutlined className='log_content_single_arrow' style={{ display: openJobLog === (index + 1) ? 'block' : 'none' }} onClick={() => openJobLog ? setOpenJobLog() : setOpenJobLog(index + 1)} />
            <p className={`log_content_single_log ${openJobLog === (index + 1) ? 'log_content_single_log_select' : ''}`}>{item}</p>
          </div>
        )) : <p className='log_content_nolog'>暂无日志</p>}
      </div>
    </div>
  </div>;
}